package com.htfyun.factorytest.agingtest;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.htfyun.factorytest.R;
import com.htfyun.factorytest.utils.DebugLog;
import com.htfyun.factorytest.utils.TimeStringUtils;

/**
 * Created by sst on 2018/10/30.
 */

public abstract class AgingTestItemBase implements Comparable<AgingTestItemBase> {

    private final Context context;

    private AgingTestItemHandler handler;

    private LinearLayout linearLayoutContainer;

    private TextView txtItemInfo;

    private volatile boolean isTestStarted = false;
    private final Object lock = new Object();


    public AgingTestItemBase(@NonNull Context context, @NonNull View view) {
        this.context = context;

        txtItemInfo = view.findViewById(R.id.txtItemInfo);

        linearLayoutContainer = (LinearLayout) view.findViewById(R.id.linearLayoutContainer);

        handler = new AgingTestItemHandler(this);
    }

    public boolean isTestStarted() {

        synchronized (lock) {
            return isTestStarted;
        }

    }

    protected void setTestStarted(boolean started) {
        this.isTestStarted = started;
    }

    public void startTest() {

        synchronized (lock) {
            if (isTestStarted()) {
                return;
            }

            setTestStarted(true);
        }

        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            switch (getAgingTestItemAttr()) {
                case sleep:
                    activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
                        activity.setShowWhenLocked(true);
                    }
                    DebugLog.e("clearFlags FLAG_KEEP_SCREEN_ON");
                    break;
                case hardware:
                case reboot:
                case emmc_cpu:
                case memory:
                default:
                    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                    break;
            }
        }


        setItemStartTime();

        doStartTest();
    }

    public void stopTest() {

        synchronized (lock) {
            if (!isTestStarted()) {
                return;
            }

            setTestStarted(false);

        }

        setItemExitTime();

        doStopTest();
    }

    public void clearRecord() {
        if (getAgingTestItemAttr() != null) {
            getAgingTestItemAttr().clearAll(getContext());
        }
    }


    protected void updateTxtItemInfo(String info) {
        if (txtItemInfo != null) {
            txtItemInfo.setText(info);
        }
    }

    protected String getTxtItemInfo() {
        if (txtItemInfo != null) {
            return txtItemInfo.getText().toString();
        }
        return "";
    }

    @NonNull
    protected Context getContext() {
        return context;
    }

    @NonNull
    protected LinearLayout getLinearLayoutContainer() {
        return linearLayoutContainer;
    }

    @NonNull
    protected AgingTestItemHandler getHandler() {
        return handler;
    }


    @Override
    public int compareTo(AgingTestItemBase o) {
        if (this.getAgingTestItemAttr() == null) {
            return 0;
        }
        if (o.getAgingTestItemAttr() == null) {
            return 0;
        }

        return this.getAgingTestItemAttr().getId() - o.getAgingTestItemAttr().getId();
    }


    private void setItemStartTime() {
        if (getAgingTestItemAttr() == null) {
            return;
        }

        if (getAgingTestItemAttr().getStartTime_ms(getContext()) <= 0
                || !AgingTestItemAttr.isAgingKeepRecord(getContext())) {

            long tick = AgingTestItemAttr.getAgingTimeTick_ms(getContext());
            String nowStr = TimeStringUtils.makeTimeString(tick);

            DebugLog.e("setItemStartTime -> item = " + getAgingTestItemAttr().getNominalName(getContext()) + ", nowStr = " + nowStr);
            getAgingTestItemAttr().setStartTime_ms(getContext(), tick);
            getAgingTestItemAttr().setStartTimeAbsolute_ms(getContext(), System.currentTimeMillis());
            getAgingTestItemAttr().setStartTimeCurrent(getContext(), TimeStringUtils.getCurrentFormatTime());
        }


    }

    private void setItemExitTime() {
        if (getAgingTestItemAttr() == null) {
            return;
        }

        long tick = AgingTestItemAttr.getAgingTimeTick_ms(getContext());
        String nowStr = TimeStringUtils.makeTimeString(tick);

        DebugLog.e("setItemExitTime -> item = " + getAgingTestItemAttr().getNominalName(getContext()) + ", nowStr = " + nowStr);

        getAgingTestItemAttr().setExitTime_ms(getContext(), tick);
        getAgingTestItemAttr().setExitTimeAbsolute_ms(getContext(), System.currentTimeMillis());

    }

    protected abstract void handleMessage(Message msg);

    protected abstract void doStartTest();

    protected abstract void doStopTest();

    public abstract AgingTestItemAttr getAgingTestItemAttr();

}
